
using System;
using System.Collections.Generic;

namespace cachier
{


	public class GetCommand : Command
	{
		string[] line;
		
		public GetCommand (string[] line)
		{
			this.line = line;
		}
		
		public bool Process() 
		{
			if (line.Length != 3) return false;
			
			int amount = int.Parse(line[2]);
			String currency = line[1].ToUpper();
			
			CurrencyHandler handler;
			if (Storage.getCurrencyMap().ContainsKey(currency) && amount >= 0 ) 
				handler = Storage.getCurrencyMap()[currency];
			else 
				return false;
			
			Dictionary<int, int> output = new Dictionary<int, int>();
			for (int i = handler.GetNominalsLength() - 1; i >= 0 || amount <= 0; i--) {
				int number = amount / handler.ShowNominal(i);
				
				if (number >= handler.ShowAmount(i)) {
					output.Add(handler.ShowNominal(i), number);
					amount %= handler.ShowNominal(i);
				} else {
					if (handler.ShowAmount(i) > 0) {
						output.Add(handler.ShowNominal(i), handler.ShowAmount(i));
						amount -= handler.ShowAmount(i);
					}
				}
			}
			
			if (amount <= 0) {
				//Storage.SetCurrencyOutput(output);
				handler.Process(output);
				return true;
			} else
				return false;
		}
	}
}